package vcp.client.GUI.mvc.controller;

import javax.swing.SwingUtilities;

import vcp.client.GUI.events.FormSubmitionEvent;
import vcp.client.GUI.mvc.model.UIAbstractModel;
import vcp.client.GUI.mvc.model.UISubscriptionModel;
import vcp.client.GUI.mvc.view.UIAbstractView;
import vcp.client.GUI.mvc.view.UISubscriptionView;
import vcp.client.forms.SubscriptionForm;
import vcp.client.interfaces.listeners.IFormSubmitionListener;
import vcp.entity.Customer;
import vcp.enums.PopUpMessageType;
import vcp.enums.Screen;

/**
 * 
 * @author VCP team
 *         <p>
 *         Subscription controller
 *         </p>
 */
public class UISubscriptionController extends UIAbstractController implements IFormSubmitionListener <SubscriptionForm>
{
	/** reference to the view **/
	private final UISubscriptionView view;
	/** reference to the model **/
	private final UISubscriptionModel model;

	/**
	 * 
	 * @param view
	 *            to screen
	 * @param model
	 *            update with data
	 */
	public UISubscriptionController ( UISubscriptionView view, UISubscriptionModel model )
	{
		super(Screen.SUBSCRIBER);
		this.view = view;
		this.model = model;
		this.setupListeners();
	}

	// ****************************************************************************************************************************
	// UIAbstractController methods
	// ****************************************************************************************************************************
	@Override
	public UIAbstractView getView ()
	{
		return this.view;
	}

	@Override
	public UIAbstractModel getModel ()
	{
		return this.model;
	}

	@Override
	public void clearMVC ()
	{
		this.view.setOnScreen(false);
	}

	@Override
	public void uploadDataContext ( Object data )
	{
		if ( data instanceof Customer )
		{
			this.flashData();
			Customer customer = (Customer) data;
			this.model.setCustomer(customer);
			this.view.loadSusbscriptions(this.model.getCustomerSubscriptionIDs());
			this.view.loadCustomerID(this.model.getCustomer().getCustomerID());
			this.view.resetDates(this.parentDelegate.requestCurrentClientDateTime());
			this.view.setOnScreen(true);
		}
	}

	@Override
	public void flashData ()
	{
		this.view.clearView();
		this.model.clearModel();
	}

	@Override
	protected void setupListeners ()
	{
		super.setupListeners();
		this.view.setFormSubmitionListener(this);
	}

	@Override
	protected synchronized void reloadGUI ()
	{
		this.view.setOnScreen(false);
		this.view.loadSusbscriptions(UISubscriptionController.this.model.getCustomerSubscriptionIDs());
		this.view.setOnScreen(true);
	}

	// ****************************************************************************************************************************
	// IControllerModelDelegate methods
	// ****************************************************************************************************************************
	@Override
	public void updateControllerDelegateWithData ( Object newData )
	{
		this.parentDelegate.notifyOnDataUpdate(this.model.getCustomer());
		if ( this.view.isOnScreen() )
		{
			if ( newData == null )
			{
				SwingUtilities.invokeLater(new Runnable()
					{
						@Override
						public void run ()
						{
							UISubscriptionController.this.reloadGUI();
						}
					});
			}
			else if ( newData instanceof String )
			{
				String notification = (String) newData;
				this.parentDelegate.showPopUpMessage(notification, PopUpMessageType.INFORMATION);
			}
		}
	}

	// ****************************************************************************************************************************
	// IFormSubmitionListener methods
	// ****************************************************************************************************************************
	@Override
	public void onFormSubmitted ( FormSubmitionEvent <SubscriptionForm> event )
	{
		this.model.applyForm(event.getForm());
	}

}
